package cn.com.wangluotongxin.linux.controller;

import cn.com.wangluotongxin.common.web.AjaxResult;
import cn.com.wangluotongxin.linux.constants.FileTypeEnum;
import cn.com.wangluotongxin.linux.form.CreateFileForm;
import cn.com.wangluotongxin.linux.form.DeleteFileForm;
import cn.com.wangluotongxin.linux.form.ReadFileForm;
import cn.com.wangluotongxin.linux.form.WriteFileForm;
import cn.com.wangluotongxin.linux.service.IFileService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "文件")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IFileService fileService;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("filePath") String filePath) {
        Assert.isTrue(!file.isEmpty(), "请选择文件");
        Assert.isTrue(StrUtil.isNotBlank(filePath), "缺少上传路径");

        fileService.uploadFile(file, filePath);
        return AjaxResult.success("上传成功");
    }

    @ApiOperation("创建文件")
    @PostMapping("/create")
    public AjaxResult createFile(@RequestBody CreateFileForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getFilePath()), "缺少路径");
        Assert.isTrue(StrUtil.isNotBlank(form.getFileName()), "缺少文件名称");
        Assert.notNull(form.getFileType(), "缺少文件类型");
        FileTypeEnum fileTypeEnum = FileTypeEnum.getByCode(form.getFileType());
        Assert.notNull(fileTypeEnum, "不支持的文件类型");

        fileService.createFile(form.getFilePath(), form.getFileName(), fileTypeEnum);
        return AjaxResult.success("创建成功");
    }

    @ApiOperation("删除文件")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody DeleteFileForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getFilePath()), "缺少路径");
        Assert.isTrue(StrUtil.isNotBlank(form.getFileName()), "缺少文件名称");

        fileService.removeFile(form.getFilePath(), form.getFileName());
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("阅读文件")
    @PostMapping("/read")
    public AjaxResult<Object> readFile(@RequestBody ReadFileForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getFilePath()), "缺少路径");
        Assert.isTrue(StrUtil.isNotBlank(form.getFileName()), "缺少文件名称");

        return AjaxResult.success(fileService.readFile(form.getFilePath(), form.getFileName()));
    }

    @ApiOperation("写入文件内容")
    @PostMapping("/write")
    public AjaxResult writeFile(@RequestBody WriteFileForm form) {
        Assert.isTrue(StrUtil.isNotBlank(form.getFilePath()), "缺少路径");
        Assert.isTrue(StrUtil.isNotBlank(form.getFileName()), "缺少文件名称");

        fileService.writeFile(form.getFilePath(), form.getFileName(), form.getContent());
        return AjaxResult.success();
    }
}
